<template>
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" v-dialogDrag :visible.sync="visible">
			<el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading"
				:class="method==='view'?'readonly':''" :disabled="method==='view'" label-width="120px">
				<el-row :gutter="15">
					<el-col :span="12">
						<el-form-item label="使用人" prop="user.id" :rules="[
                  {required: true, message:'使用人不能为空', trigger:'blur'}
                 ]">
							<user-select :limit='1' :value="inputForm.user.id"
								@getValue='(value) => {inputForm.user.id=value}'></user-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="公司" prop="company.id" :rules="[
                  {required: true, message:'公司不能为空', trigger:'blur'}
                 ]">
							<SelectTree ref="company" :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'children'    // 子级字段名
                        }" url="/sys/office/treeData?type=2" :value="inputForm.company.id" :clearable="true"
								:accordion="true" @getValue="(value) => {inputForm.company.id=value}" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="部门" prop="depart.id" :rules="[
                  {required: true, message:'部门不能为空', trigger:'blur'}
                 ]">
							<SelectTree ref="depart" :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'children'    // 子级字段名
                        }" url="/sys/office/treeData?type=2" :value="inputForm.depart.id" :clearable="true"
								:accordion="true" @getValue="(value) => {inputForm.depart.id=value}" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="电话" prop="phone" :rules="[
                  {required: true, message:'电话不能为空', trigger:'blur'}
                 ]">
							<el-input v-model="inputForm.phone" placeholder="请填写电话"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="固定资产" prop="fixedAssets" :rules="[
                  {required: true, message:'固定资产不能为空', trigger:'blur'}
                 ]">
							<el-input v-model="inputForm.fixedAssets" placeholder="请填写固定资产"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="使用时间" prop="userTime" :rules="[
                  {required: true, message:'使用时间不能为空', trigger:'blur'}
                 ]">
							<el-input v-model="inputForm.userTime" placeholder="请填写使用时间"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="归还时间" prop="remandTime" :rules="[
                 ]">
							<el-input v-model="inputForm.remandTime" placeholder="请填写归还时间"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="归还状态" prop="remandType" :rules="[
                  {required: true, message:'归还状态不能为空', trigger:'blur'}
                 ]">
							<el-select v-model="inputForm.remandType" placeholder="请选择" style="width: 100%;">
								<el-option v-for="item in $dictUtils.getDictList('remand_type')" :key="item.value"
									:label="item.label" :value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="备注信息" prop="remarks" :rules="[
                 ]">
							<el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button size="small" @click="visible = false">关闭</el-button>
				<el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定
				</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import UserSelect from '@/components/userSelect'
	import SelectTree from '@/components/treeSelect/treeSelect.vue'
	export default {
		data() {
			return {
				title: '',
				method: '',
				visible: false,
				loading: false,
				inputForm: {
					id: '',
					user: {
						id: ''
					},
					company: {
						id: ''
					},
					depart: {
						id: ''
					},
					phone: '',
					fixedAssets: '',
					userTime: '',
					remandTime: '',
					remandType: '',
					remarks: ''
				}
			}
		},
		components: {
			UserSelect,
			SelectTree
		},
		methods: {
			init(method, id) {
				this.method = method
				this.inputForm.id = id
				if (method === 'add') {
					this.title = `新建物资领用与固定资产中间表`
				} else if (method === 'edit') {
					this.title = '修改物资领用与固定资产中间表'
				} else if (method === 'view') {
					this.title = '查看物资领用与固定资产中间表'
				}
				this.visible = true
				this.loading = false
				this.$nextTick(() => {
					this.$refs.inputForm.resetFields()
					if (method === 'edit' || method === 'view') { // 修改或者查看
						this.loading = true
						this.$http({
							url: `/dlyrl/oa/assetsmaterial/assetsMaterial/queryById?id=${this.inputForm.id}`,
							method: 'get'
						}).then(({
							data
						}) => {
							this.inputForm = this.recover(this.inputForm, data.assetsMaterial)
							this.loading = false
						})
					}
				})
			},
			// 表单提交
			doSubmit() {
				this.$refs['inputForm'].validate((valid) => {
					if (valid) {
						this.loading = true
						this.$http({
							url: `/dlyrl/oa/assetsmaterial/assetsMaterial/save`,
							method: 'post',
							data: this.inputForm
						}).then(({
							data
						}) => {
							this.loading = false
							if (data && data.success) {
								this.visible = false
								this.$message.success(data.msg)
								this.$emit('refreshDataList')
							}
						})
					}
				})
			}
		}
	}
</script>
